package com.bnyy.gbp.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/6/9
 **/
public class LaunchAppService extends Service {

    public static void startAndKillSelf(Context context, String packageName) {
        Log.e("LaunchAppService", "startAndKillSelf");
        Intent intent = new Intent(context, LaunchAppService.class);
        intent.putExtra("packageName", packageName);
        context.startService(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String packageName = intent.getStringExtra("packageName");
        if (!TextUtils.isEmpty(packageName)) {
            Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
            startActivity(LaunchIntent);
            stopSelf();
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
